import Decimal from 'decimal.js';

/**
 * 将长字符串转化成...的形式
 * @param {Number} max 最多显示的字符串
 */
export const strOverflow = (value, max) => {
  if (!value) return '';
  const valueStr = value.toString();
  if (valueStr.length <= max) return valueStr;
  return valueStr.substring(0, max) + '...';
};

/**
 * 将秒转化成XX:XX的形式
 */
export const formatSec = (value) => {
  const val = parseInt(value, 0);
  if (!val) return '00:00';
  let minute = parseInt(val / 60, 0);
  let second = val - minute * 60;
  minute = minute < 10 ? `0${minute}` : minute;
  second = second < 10 ? `0${second}` : second;
  return `${minute}:${second}`;
};

export const amountFormat = (value) => {
  const amount = Decimal(value || 0).toFixed(2);
  let newAmount = '0.00';
  const reg = /(\d)(?=(\d{3})+\.)/g;
  if (!isNaN(amount)) {
    if (typeof amount === 'number' || typeof amount === 'string') {
      newAmount = parseFloat(amount).toFixed(2).replace(reg, '$1,');
    }
  }
  return newAmount;
};
